package gdufs.lkl.calculator;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;

public class ConverterActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_converter);
        
        // 设置工具栏
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        
        // 设置卡片点击事件
        findViewById(R.id.cardCurrency).setOnClickListener(this);
        findViewById(R.id.cardLength).setOnClickListener(this);
        findViewById(R.id.cardArea).setOnClickListener(this);
        findViewById(R.id.cardVolume).setOnClickListener(this);
        findViewById(R.id.cardTemperature).setOnClickListener(this);
        findViewById(R.id.cardSpeed).setOnClickListener(this);
        findViewById(R.id.cardTime).setOnClickListener(this);
        findViewById(R.id.cardWeight).setOnClickListener(this);
        findViewById(R.id.cardNumeral).setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        int converterType = -1;
        
        int id = v.getId();
        if (id == R.id.cardCurrency) {
            converterType = UnitConverterActivity.TYPE_CURRENCY;
        } else if (id == R.id.cardLength) {
            converterType = UnitConverterActivity.TYPE_LENGTH;
        } else if (id == R.id.cardArea) {
            converterType = UnitConverterActivity.TYPE_AREA;
        } else if (id == R.id.cardVolume) {
            converterType = UnitConverterActivity.TYPE_VOLUME;
        } else if (id == R.id.cardTemperature) {
            converterType = UnitConverterActivity.TYPE_TEMPERATURE;
        } else if (id == R.id.cardSpeed) {
            converterType = UnitConverterActivity.TYPE_SPEED;
        } else if (id == R.id.cardTime) {
            converterType = UnitConverterActivity.TYPE_TIME;
        } else if (id == R.id.cardWeight) {
            converterType = UnitConverterActivity.TYPE_WEIGHT;
        } else if (id == R.id.cardNumeral) {
            converterType = UnitConverterActivity.TYPE_NUMERAL;
        }
        
        if (converterType != -1) {
            Intent intent = new Intent(this, UnitConverterActivity.class);
            intent.putExtra(UnitConverterActivity.EXTRA_CONVERTER_TYPE, converterType);
            startActivity(intent);
        }
    }
      @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        
        if (id == android.R.id.home) {
            finish();
            return true;
        } else if (id == R.id.action_switch_to_calculator) {
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
            return true;
        } else if (id == R.id.action_switch_to_scientific) {
            Intent intent = new Intent(this, ScientificCalculatorActivity.class);
            startActivity(intent);
            finish();
            return true;
        } else if (id == R.id.action_switch_to_converter) {
            // 已经在换算页面，不操作
            return true;
        } else if (id == R.id.action_custom_function) {
            Intent intent = new Intent(this, CustomFunctionManagerActivity.class);
            startActivity(intent);
            return true;
        } else if (id == R.id.action_history) {
            Toast.makeText(this, "历史记录功能待实现", Toast.LENGTH_SHORT).show();
            return true;
        } else if (id == R.id.action_settings) {
            Toast.makeText(this, "设置功能待实现", Toast.LENGTH_SHORT).show();
            return true;
        }
        
        return super.onOptionsItemSelected(item);
    }
}
